package com.zuikaku.job;

import com.aliyuncs.IAcsClient;
import com.aliyuncs.alidns.model.v20150109.DescribeSubDomainRecordsRequest;
import com.aliyuncs.alidns.model.v20150109.DescribeSubDomainRecordsResponse;
import com.aliyuncs.alidns.model.v20150109.UpdateDomainRecordRequest;
import com.zuikaku.utils.AliyunDDNS;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class DetectIpChange implements Runnable {
    private String domain;

    private IAcsClient aliyunClient;

    private int periodTime;

    public int getPeriodTime() {
        return periodTime;
    }

    public void setPeriodTime(int periodTime) {
        this.periodTime = periodTime;
    }

    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    public IAcsClient getAliyunClient() {
        return aliyunClient;
    }

    public void setAliyunClient(IAcsClient aliyunClient) {
        this.aliyunClient = aliyunClient;
    }

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public void run() {
        while (true) {
            //1:查询指定二级域名的最新解析记录
            DescribeSubDomainRecordsRequest describeSubDomainRecordsRequest = new DescribeSubDomainRecordsRequest();
            describeSubDomainRecordsRequest.setSubDomain(domain);
            DescribeSubDomainRecordsResponse describeSubDomainRecordsResponse = AliyunDDNS.describeSubDomainRecords(describeSubDomainRecordsRequest, aliyunClient);
            List<DescribeSubDomainRecordsResponse.Record> domainRecords = describeSubDomainRecordsResponse.getDomainRecords();
            if (domainRecords.size() != 0) {
                DescribeSubDomainRecordsResponse.Record record = domainRecords.get(0);
                //  记录ID
                String recordId = record.getRecordId();
                //  记录值
                String recordsValue = record.getValue();
                //  2:当前主机公网IP
                String currentHostIP = AliyunDDNS.getCurrenHostIP();
                System.out.println("--"+sdf.format(new Date())+" 当前主机公网IP为：" + currentHostIP);
                if (!currentHostIP.equals(recordsValue)) {
                    //3:修改解析记录
                    UpdateDomainRecordRequest updateDomainRecordRequest = new UpdateDomainRecordRequest();
                    //  主机记录
                    updateDomainRecordRequest.setRR(domain.substring(0, domain.indexOf(".")));
                    //  记录ID
                    updateDomainRecordRequest.setRecordId(recordId);
                    //  将主机记录值改为当前主机IP
                    updateDomainRecordRequest.setValue(currentHostIP);
                    //  解析记录类型
                    updateDomainRecordRequest.setType("A");
                    AliyunDDNS.updateDomainRecord(updateDomainRecordRequest, aliyunClient);
                    System.out.println("--"+sdf.format(new Date())+" 执行公网IP变更：" + recordsValue + "--->" + currentHostIP);
                    String nowIp = AliyunDDNS.describeSubDomainRecords(describeSubDomainRecordsRequest, aliyunClient).getDomainRecords().get(0).getValue();
                    if (nowIp.equals(currentHostIP)) {
                        System.out.println("--"+sdf.format(new Date())+" 成功：公网IP已变更：" + recordsValue + "--->" + currentHostIP);
                    } else {
                        System.err.println("--"+sdf.format(new Date())+" 失败：公网IP未变更：" + recordsValue + "-X->" + currentHostIP);
                    }
                } else {
                    System.out.println("--"+sdf.format(new Date())+" IP尚未变更");
                }
            } else {
                System.err.println(domain + " 该域名不存在解析记录");
            }
            try {
                Thread.sleep(periodTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
